﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics.Contracts;
using System.Windows.Media;
using System.Windows;
using System.Windows.Data;

namespace ShredTheLog.Converters
{
    public sealed class RectangleGeometryConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null && values.Length == 4 && values[0] is double && values[1] is double && values[2] is double && values[3] is double)
            {
                var width = (double)values[0];
                var height = (double)values[1];
                var cornerRadius = (double)values[2];
                var borderThickness = (double)values[3];
                double rectWidth = width - borderThickness * 2.0;
                double rectHeight = height - borderThickness * 2.0;
                if (rectWidth >= 0.0 && rectHeight >= 0.0)
                {
                    Contract.Assume(rectWidth >= 0.0);
                    Contract.Assume(rectHeight >= 0.0);
                    return new RectangleGeometry(new Rect(0.0, 0.0, rectWidth, rectHeight), cornerRadius, cornerRadius);
                }
                else
                {
                    return null;
                }
            }
            else
                return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }

        public static readonly RectangleGeometryConverter Default = new RectangleGeometryConverter();
    }
}
